﻿#ifndef DMC_B140_M_H
#define DMC_B140_M_H


#include <QtNetwork>
#include <QTimer>
#include <QQueue>
#include "motioncontroller.h"

class DMC_B140_M: public MotionController
{
    Q_OBJECT
public:
    explicit DMC_B140_M(QObject *parent = 0);
    ~DMC_B140_M();
    void init() override;
    void setSpeed(unsigned short axis, int speed) override;
    void setAcceleration(unsigned short axis, int acceleration) override;
    void setDeceleration(unsigned short axis, int deceleration) override;
    void moveTo(unsigned short axis, int position) override;
    void move(unsigned short axis, int dir = 1) override;
    void moveSteps(unsigned short axis, int steps) override;
    void stop(unsigned short axis) override;
    void abort() override;
    int readTheoryPos(unsigned short axis) override;
    bool readTheoryPosAll(int *value) override;
    int readEncodePos(unsigned short axis) override;
    bool readEncodePosAll(int *value) override;
    bool readIO(bool *value) override;
    void setStartSpeed(unsigned short axis, int startSpeed) override;
    void setStopSpeed(unsigned short axis, int stopSpeed) override;
    void setTheoryPos(unsigned short axis, long offset) override;
    void setEncoderPos(unsigned short axis, long offset) override;
    void goHome(unsigned short axis, int speed) override;
    bool isMoving(unsigned short axis) override;

    void connectViaEthernet(QString ip, int port);
    void setEmergencyInput(bool on);
    void setNetworkPara(QString ip, int port);
signals:
    void cmdTimeout();
    void connectionLost();
    void connectionSetup();

private:
    QTcpSocket * m_socket;
    QTimer m_timer;
    bool m_isInit;

    // 下面两项是网卡相关的参数
    QString m_ipAddress;
    int m_portNum;

    QVector<int> m_speed;//速度绝对值
    QVector<int> m_EncodePos;//光栅尺初始位置
    QVector<int> m_TheoryPos;//理论初始位置
    QVector<bool> m_IO;//通用IO输入状态

    /// 用于不需要返回值的命令
    bool cmdV(QString cmd);

    bool cmdD(QString cmd, double *value);
    /// 用于返回一个 int 值的命令
    bool cmdI(QString cmd, int *value);

    /// 用于返回 4 个int 值的命令
    bool cmdI4(QString cmd, int *value);

    /// 用于返回 8 个bool 值的命令
    bool cmdB8(QString cmd, bool *value);
private slots:
    void initBasicPara();
    void disconnected();
};

#endif // DMC_B140_M_H
